﻿using CodeGenerator.CodeGenBL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeGenerator.Models
{
    public class ClassModel
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public List<AttributeModel> _attributes;
        public int ProjectId { get; set; }

        public ClassModel(ClassModelDto dto)
        {
            Id = dto.Id;
            Name = dto.Name;	
            ProjectId = dto.ProjectId;

			_attributes = new List<AttributeModel>();

            foreach (AttributeModelDto attridto in dto.Attributes)
            {
                AttributeModel attributeModel = new AttributeModel(attridto);
                _attributes.Add(attributeModel);
            }
        }

        public ClassModel()
        {
            _attributes = new List<AttributeModel>();
        }

        public void AddAttribute(AttributeModel attribute)
        {
            _attributes.Add(attribute);
        }

        public List<AttributeModel> GetAttributes()
        {
            return _attributes;
        }

        public ClassModel GetAllPrimaryKey()
        {
            ClassModel newDto = new ClassModel();
            newDto.Id = this.Id;
            newDto.Name = this.Name;

            newDto._attributes = _attributes.FindAll(a => a.Pk == true);

            return newDto;
        }

        public ClassModel GetAllSearchable()
        {
            ClassModel newDto = new ClassModel();
            newDto.Id = this.Id;
            newDto.Name = this.Name;

            newDto._attributes = _attributes.FindAll(a => a.Search == true);

            return newDto;
        }

        public ClassModel GetAllDisplay()
        {
            ClassModel newDto = new ClassModel();
            newDto.Id = this.Id;
            newDto.Name = this.Name;

            newDto._attributes = _attributes.FindAll(a => a.Display == true);

            return newDto;
        }

        public ClassModel GetAllDisplayInGrid()
        {
            ClassModel newDto = new ClassModel();
            newDto.Id = this.Id;
            newDto.Name = this.Name;

            newDto._attributes = _attributes.FindAll(a => a.DisplayLengthInGrid > 0);

            return newDto;
        }

        public ClassModel GetAllNotNullable()
        {
            ClassModel newDto = new ClassModel();
            newDto.Id = this.Id;
            newDto.Name = this.Name;

            newDto._attributes = _attributes.FindAll(a => a.Nullable == false);

            return newDto;
        }

        public ClassModel GetAllNullable()
        {
            ClassModel newDto = new ClassModel();
            newDto.Id = this.Id;
            newDto.Name = this.Name;

            newDto._attributes = _attributes.FindAll(a => a.Nullable == true);

            return newDto;
        }

        /*public ClassModel GetAllDataType(int dataTypeId)
        {
            ClassModel newDto = new ClassModel();
            newDto.Id = this.Id;
            newDto.Name = this.Name;

            newDto._attributes = _attributes.FindAll(a => a.DataType.TypeId == dataTypeId);

            return newDto;
        }

        public ClassModel GetAllFieldType(int fieldTypeId)
        {
            ClassModel newDto = new ClassModel();
            newDto.Id = this.Id;
            newDto.Name = this.Name;

            newDto._attributes = _attributes.FindAll(a => a.FieldType.TypeId == fieldTypeId);

            return newDto;
        }*/

        public ClassModel GetAllSearchResult()
        {
            ClassModel newDto = new ClassModel();
            newDto.Id = this.Id;
            newDto.Name = this.Name;

            newDto._attributes = _attributes.FindAll(a => a.SearchResult == true);

            return newDto;
        }

        public ClassModel GetAllDateTime() //Apple Add Method
        {
            ClassModel newDto = new ClassModel();
            newDto.Id = this.Id;
            newDto.Name = this.Name;

            newDto._attributes = _attributes.FindAll(a => a.DataType.Name == "DateTime");

            return newDto;
        }
        public ClassModel GetAllLookup() //Apple Add Method
        {
            ClassModel newDto = new ClassModel();
            newDto.Id = this.Id;
            newDto.Name = this.Name;

            newDto._attributes = _attributes.FindAll(a => a.DataType.Name == "Lookup Table");

            return newDto;
        }
    }
}
